<?php
/**
 * Created by PhpStorm.
 * User: huihui
 * Date: 2019/8/19
 * Time: 2:46 PM
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use think\Cache;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;

class Domain extends Frontend
{

    protected $layout = 'default';
    protected $noNeedLogin = ['openid'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        //监听注册登录注销的事件
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = input('post.keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        Hook::add('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        Hook::add('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        Hook::add('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
    }


    public function index()
    {

    }

    public function domain()
    {
        $user = $this->auth->getUser();
        if ($user['vip_expire_time'] == null || time()>strtotime($user['vip_expire_time'])){
            $this->error("服务到期，无法查看");
        }
        $model = new \app\admin\model\Domain();
        $domain = $model->where(['uid'=>$user->id,'is_main_switch'=>1])->order('id','desc')->find();
	$domains = $model->where(['uid'=>$user->id,'is_main_switch'=>0])->select();
	$this->assign("zip","47.116.66.253");
        $this->assign('main_domain',$domain?$domain->domain:'');
        $this->assign('main_domain_status',$domain?$domain->status:'主域名未配置');
        $this->assign('domains',$domains);
        $this->assign('user',$user);
        $this->assign('title',"域名配置");
        return view();
    }


    public function gzhdomain()
    {
        $user = $this->auth->getUser();
        if ($user['vip_expire_time'] == null || time()>strtotime($user['vip_expire_time'])){
            $this->error("服务到期，无法查看");
        }
//        $result = array("domain"=>$user["domain"], "verifyfile"=>$user["verifyfile"], "appid"=>$user["wxappid"], "appSecret"=>$user["AppSecret"], "wxmchid"=>$user["wxmchid"]);
        return view()->assign("users", $user);
    }

    public function getOpenid()
    {
        $user = $this->auth->getUser();
        $domain = new \app\admin\model\Domain();
        $return_url = "http://".$user['domain']."/index/domain/openid/userid/".$user['id'];
//        echo $return_url;die;
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$user['wxappid']."&redirect_uri=".urlencode($return_url)."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";

        $this->redirect(url('/index/util/qrcode_base64/',['str'=>base64_encode($url)]));
    }

    public function openid()
    {
        $user = \app\admin\model\User::get(input('userid'));
        if ($code = input('code')){
            $acc_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$user['wxappid']."&secret=".$user['AppSecret']."&code=".$code."&grant_type=authorization_code";
            $acc_data = json_decode(file_get_contents($acc_url),true);
            $access_token = $acc_data['access_token'];
            $openid = $acc_data['openid'];
            $user->save($user['openid'] = $openid);
            $this->success("绑定成功，请刷新网页。",'http://www.qq.com');
        }
    }
}
